<?php
namespace app\model;

use think\Model;
use app\model\BbsType;
use app\model\User;
use app\model\BbsLike;
use app\model\BbsComment;
use app\model\UserFans;

class Bbs extends Model
{
    protected $table = "ea_bbs";

    /**
     * [getBbsInfo 获取一条动态,主要用于点击查看bbs详细]
     * @since  2022-04-27
     * @author Dai哥
     * @param  [type]     $param [description]
     * @return [type]            [description]
     */
    public static function getBbsDetailInfo($data)
    {
        $model = self::whereNull('delete_time');

        $info = $model = $model->field('id, user_id, bbs_imgs, type_id, is_hot, browse, content, create_time')
            ->withCount([
                # 点赞总数
                'bbsLike',

                # 评论总数
                'bbsComment',

                # 判断当前登录的用户是否关注了他
                'bbFans' => function($query, &$alias) use ($data){
                    $query->where('user_id', $data['user_id']);
                    $alias='this_is_follow';
                },
            ])
            ->with([
                // 'bbFans',
                # 所属分类
                'bbsType',

                # 作者信息
                'bbsUser',

                # 最新的5个点赞头像
                'bbsLike' => function($query) use ($data){
                    $query->alias('a')
                        ->join('ea_user b', 'b.id = a.user_id')
                        ->order('a.create_time DESC')
                        ->field('a.*, b.avatar')
                        ->limit(5);
                },

                # 判断当前用户是否有点赞
                'bbsCk' => function($query) use ($data){
                    $query->where('user_id', $data['user_id']);
                },
            ])->find($data['bbs_id']);

        // dump($info->toArray());exit;
        return $info;
    }

    /**
     * [setWhere 过滤条件]
     * @since  2022-04-06
     * @author Dai哥
     * @param  [type]     $data [description]
     */
    public static function setWhere($data)
    {
        $model = self::whereNull('delete_time')->order('create_time DESC');

        # 此参正常指挥在获取详细的时候才传
        if(isset($data['id']) && $data['id']){
            $model = $model->where('id', $data['id']);
        }

        if(isset($data['mval']) && $data['mval']){

            switch ($data['mval']) {

                # 热门
                case 'hot':
                    $model = $model->where('is_hot', 1);
                    break;

                # 我的动态
                case 'me':
                    $model = $model->where('user_id', $data['user_id']);
                    break;

                # 我的关注
                case 'follow':
                    $fans_ids = UserFans::where('user_id', $data['user_id'])->column('fans_id');
                    $userIds = implode(',', $fans_ids);
                    $model = $model->whereIn('user_id', $userIds);
                    break;
            }
        }

        if(isset($data['type_id']) && $data['type_id']){
            $model = $model->where('type_id', $data['type_id']);
        }

        // if(isset($data['user_id']) && $data['user_id']){
        //     $model = $model->where('user_id', $data['user_id']);
        // }

        $model = $model->field('id, user_id, bbs_imgs, type_id, is_hot, browse, content, create_time')
            ->withCount([
                # 点赞总数
                'bbsLike',

                # 评论总数
                'bbsComment',

                # 判断当前登录的用户是否关注了他
                'bbFans' => function($query, &$alias) use ($data){
                    $query->where('user_id', $data['user_id']);
                    $alias='this_is_follow';
                },
            ])
            ->with([
                // 'bbFans',
                # 所属分类
                'bbsType',

                # 作者信息
                'bbsUser',

                # 最新的5个点赞头像
                // 'bbsLike' => function($query) use ($data){
                //     $query->alias('a')
                //         ->join('ea_user b', 'b.id = a.user_id')
                //         ->order('a.create_time DESC')
                //         ->field('a.*, b.avatar')
                //         ->limit(5);
                // },

                # 判断当前用户是否有点赞
                'bbsCk' => function($query) use ($data){
                    $query->where('user_id', $data['user_id']);
                },

                # 评论列表
                // 'bbsComment' => function($query) use ($data){
                //     $query->where('parent_id', 0)
                //         ->field('id, bbs_id, user_id, msg, create_time')
                //         ->with([
                //             # 关联首级发布人信息
                //             'user' => function($qu) use ($data){
                //                 $qu->field('id, nickname, avatar');
                //             },

                //             # 是否有子评论内容
                //             'somCommentList' => function($somquery) use ($data){
                //                 $somquery->field('id, msg, parent_id, bbs_id, create_time, user_id')
                //                     ->with([
                //                         # 提交回复评论人的信息
                //                         'user' => function($userquery){
                //                             $userquery->field('id, nickname, avatar');
                //                         }
                //                     ])
                //                     ->order('id DESC');
                //             }
                //         ])
                //         ->limit(5)
                //         ->order('create_time DESC');
                // }
            ]);

        return $model;
    }

    /**
     * [getBbsInfo 获取bbs详细]
     * @since  2022-04-07
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getBbsInfo($data)
    {
        $detail = self::setWhere($data)->find();

        return $detail;
    }

    /**
     * [getBbsList 获取动态列表]
     * @since  2022-04-06
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getBbsList($data)
    {
        $list = self::setWhere($data)
            ->page($data['page'], $data['limit'])
            ->select();

        return $list;
    }

    /**
     * [bbsComment 关联统评论表]
     * @since  2022-04-06
     * @author Dai哥
     * @return [type]     [description]
     */
    public function bbsComment()
    {
        return $this->hasMany(BbsComment::class, 'bbs_id', 'id')->whereNull('delete_time');
    }

    /**
     * [bbsType 关联统计点赞的数量]
     * @since  2022-04-06
     * @author Dai哥
     * @return [type]     [description]
     */
    public function bbsLike()
    {
        return $this->hasMany(BbsLike::class, 'bbs_id', 'id');
    }

    /**
     * [bbsType 当前动态作者的粉丝]
     * @since  2022-04-06
     * @author Dai哥
     * @return [type]     [description]
     */
    public function bbFans()
    {
        return $this->hasMany(UserFans::class, 'fans_id', 'user_id');
    }

    /**
     * [bbsType 关联统计点赞的数量,判断当前是否有点赞,如果有就显示对应的样式]
     * @since  2022-04-06
     * @author Dai哥
     * @return [type]     [description]
     */
    public function bbsCk()
    {
        return $this->hasMany(BbsLike::class, 'bbs_id', 'id');
    }


    /**
     * [bbsType 关联动态的分类]
     * @since  2022-04-06
     * @author Dai哥
     * @return [type]     [description]
     */
    public function bbsType()
    {
        return $this->hasOne(BbsType::class, 'id', 'type_id')
            ->whereNull('delete_time')
            ->where('type_status', 1)
            ->field('id, type_name');
    }

    /**
     * [bbsType 关联动态的作者]
     * @since  2022-04-06
     * @author Dai哥
     * @return [type]     [description]
     */
    public function bbsUser()
    {
        return $this->hasOne(User::class, 'id', 'user_id')
            ->whereNull('delete_time')
            ->field('id, nickname, avatar, descs');
    }

    /**
     * [getBbsImgsAttr 将图片转为数组]
     * @since  2022-04-06
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public function getBbsImgsAttr($value)
    {
        if($value){
            return explode(',', $value);
        }

        return [];
    }

    /**
     * [getCreateTimeAttr 友好显示时间]
     * @since  2022-04-06
     * @author Dai哥
     * @param  [type]     $value [description]
     * @return [type]            [description]
     */
    public function getCreateTimeAttr($value)
    {
        return get_friend_date($value);
    }
}